package com.ohos.carbon.slice;

import com.ohos.carbon.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.text.Font;

import java.io.InputStream;
import java.util.Map;

import static com.ohos.carbon.ResourceTable.*;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class XmlFontSlice extends AbilitySlice implements Component.ClickedListener {
    private Image back_button, debug, checkbox, showCodeButton;
    private Text text_title, text1, text2, text3, text4, text5;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(Layout_ability_xmlFont);
        initView();
        initTextStyle();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("XML Font")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("XML Font")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("XML Font", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("XML Font", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("XML Font", 1);
            pf.flushSync();
        }
    }

    private void initTextStyle() {
        Object object = new Object();
        String filePath = String.format("assets/entry/resources/rawfile/font/lato_bolditalic.ttf/%s", object);
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(filePath);
        text1.setFont(new Font.Builder(String.valueOf(inputStream)).build());
        text1.setFont(Font.SERIF);

        String filePath1 = String.format("assets/entry/resources/rawfile/font/lato_italic.ttf/%s", object);
        InputStream inputStream1 = this.getClass().getClassLoader().getResourceAsStream(filePath1);
        text2.setFont(new Font.Builder(String.valueOf(inputStream1)).build());
        text2.setFont(Font.SANS_SERIF);

        String filePath2 = String.format("assets/entry/resources/rawfile/font/lato_light.ttf/%s", object);
        InputStream inputStream2 = this.getClass().getClassLoader().getResourceAsStream(filePath2);
        text3.setFont(new Font.Builder(String.valueOf(inputStream2)).build());
        text3.setFont(Font.MONOSPACE);

        String filePath3 = String.format("assets/entry/resources/rawfile/font/lato_lightitalic.ttf/%s", object);
        InputStream inputStream3 = this.getClass().getClassLoader().getResourceAsStream(filePath3);
        text4.setFont(new Font.Builder(String.valueOf(inputStream3)).build());
        text4.setFont(Font.DEFAULT_BOLD);

        String filePath4 = String.format("assets/entry/resources/rawfile/font/lato_medium.ttf/%s", object);
        InputStream inputStream4 = this.getClass().getClassLoader().getResourceAsStream(filePath4);
        text5.setFont(new Font.Builder(String.valueOf(inputStream4)).build());
        text5.setFont(Font.DEFAULT);
    }

    private void initView() {
        text1 = (Text) findComponentById(Id_text1);
        text2 = (Text) findComponentById(Id_text2);
        text3 = (Text) findComponentById(Id_text3);
        text4 = (Text) findComponentById(Id_text4);
        text5 = (Text) findComponentById(Id_text5);
        showCodeButton = (Image) findComponentById(ResourceTable.Id_code);
        back_button = (Image) findComponentById(Id_back_button);
        back_button.setClickedListener(this);
        debug = (Image) findComponentById(Id_image1);
        debug.setClickedListener(this);
        checkbox = (Image) findComponentById(Id_image2);
        checkbox.setClickedListener(this);
        text_title = (Text) findComponentById(Id_title_text);
        text_title.setText("XML Font");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(Graphic_carbon_uparrow));

        showCodeButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                present(new com.ohos.carbon.slice.CodeSlice(), intent);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == Id_back_button) {
            onBackPressed();
        } else if (component.getId() == Id_image1) {

        } else if (component.getId() == Id_image2) {

        }
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
    }
}
